package cn.itsource.order.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.order.domain.OrderAddress;
import cn.itsource.order.domain.OrderAdopt;
import cn.itsource.order.mapper.OrderAddressMapper;
import cn.itsource.order.mapper.OrderAdoptMapper;
import cn.itsource.order.service.IOrderAdoptService;
import cn.itsource.pay.contants.BusinessContants;
import cn.itsource.pay.domain.PayBill;
import cn.itsource.pay.mapper.PayBillMapper;
import cn.itsource.pay.service.IPayBillService;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.quartz.domain.QuartzJobInfo;
import cn.itsource.quartz.service.IQuartzService;
import cn.itsource.user.domain.User;
import cn.itsource.user.domain.UserAddress;
import cn.itsource.user.mapper.UserAddressMapper;
import cn.itsource.utils.CodeGenerateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class OrderAdoptServiceImpl extends BaseServiceImpl<OrderAdopt> implements IOrderAdoptService  {


    @Autowired
    private PetMapper petMapper;

    @Autowired
    private OrderAdoptMapper orderAdoptMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private IPayBillService payBillService;

    @Autowired
    private IQuartzService quartzService;
    /*
        address_id	1
        payment_way	"AliPay"
        pet_id	60

        1. 把对应宠物下架

        2. 组装订单数据（t_order_adopt）

        3. 生成对应的地址（t_order_address）

        4. 给对应的宠物店的店主发送短信 提示

        5. 生成支付单（t_pay_bill）
     */
    @Override
    @Transactional
    public String createOrder(Map<String, String> map,User user) {
        //从map里面取出所有的数据
        //购买的宠物的id
        Long petId = Long.valueOf(map.get("pet_id"));
        //配送的地址
        Long addressId = Long.valueOf(map.get("address_id"));
        //支付的方式
        String payment_way = map.get("payment_way");

        //1. 把对应宠物下架
        Pet pet = petMapper.loadByIdOfFront(petId);
        //下架的状态
        pet.setState(0);
        //下架的时间
        pet.setOffsaletime(new Date());
        petMapper.update(pet);

        //组装订单对象
        OrderAdopt orderAdopt = createOrderAdopt(pet,user);
        orderAdoptMapper.save(orderAdopt);

        //订单对应的地址
        //根据用户选择的配送地址id，查询对应配送地址的详情
        UserAddress userAddress = userAddressMapper.loadById(addressId);

        OrderAddress orderAddress = userAddress2OrderAddress(userAddress);
        //补全信息
        orderAddress.setOrder_id(orderAdopt.getId());//orderAdopt在保存的时候要返回新增数据的主键
        orderAddress.setOrderSn(orderAdopt.getOrderSn());
        //保存信息
        orderAddressMapper.save(orderAddress);

        PayBill payBill =createPayBill(orderAdopt);
        //补全支付方式
        payBill.setPayChannel(payment_way);
        //用来 快速定位订单类型
        payBill.setBusinessType(BusinessContants.ORDER_ADOPT);
        payBill.setBusinessKey(orderAdopt.getId());
        //生成支付单
        payBillService.save(payBill);
        //在支付宝支付订单
        String result = payBillService.pay(payBill);
        //添加定时任务
        QuartzJobInfo jobInfo = new QuartzJobInfo();
        //定时任务的名字
        jobInfo.setJobName(payBill.getOrderSn());
        Map<String,Object> param = new HashMap<>();
        param.put("orderSn",payBill.getOrderSn());
        //携带的参数
        jobInfo.setParams(param);
        //设置定时任务触发的时间
        jobInfo.setFireDate(payBill.getLastPayTime());

        quartzService.addJob(jobInfo);
        return  result;

    }

    @Override
    public OrderAdopt queryByOrderSn(String orderSn) {
        return orderAdoptMapper.queryByOrderSn(orderSn);
    }

    //封装支付单对象
    private PayBill createPayBill(OrderAdopt orderAdopt) {
        PayBill payBill = new PayBill();
        payBill.setDigest(orderAdopt.getDigest());
        payBill.setMoney(orderAdopt.getPrice());
//        /*默认0:待支付  1支付成功  -1:取消订单 */
        payBill.setState(0);
        payBill.setLastPayTime(orderAdopt.getLastPayTime());
//        private String payChannel;
        payBill.setCreateTime(new Date());
        payBill.setUser_id(orderAdopt.getUser_id());
//        /*业务类型*/
//        private String businessType;
//        /*业务主键*/
//        private Long businessKey;
        payBill.setShop_id(orderAdopt.getShop_id());
        payBill.setOrderSn(orderAdopt.getOrderSn());

        return  payBill;
    }

    //封装订单地址的配送信息表 t_orderAddress
    private OrderAddress userAddress2OrderAddress(UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        //拷贝属性
        BeanUtils.copyProperties(userAddress,orderAddress);
        orderAddress.setCreateTime(new Date());
        return  orderAddress;
    }

    //封装创建订单的代码
    private OrderAdopt createOrderAdopt(Pet pet, User user) {
        OrderAdopt orderAdopt = new OrderAdopt();
        orderAdopt.setDigest("【领养订单】");
        orderAdopt.setState(0);//默认是待支付状态
        orderAdopt.setPrice(pet.getSaleprice());
        String orderSn = CodeGenerateUtils.generateOrderSn(user.getId());
        //系统生成自己的订单号 不重复
        orderAdopt.setOrderSn(orderSn);
        //最后的支付时间
        orderAdopt.setLastPayTime(new Date(System.currentTimeMillis()+60*1000*1));
//        private Long pet_id;
        orderAdopt.setPet_id(pet.getId());
//        private Long user_id;
        orderAdopt.setUser_id(user.getId());
//        private Long shop_id;
        orderAdopt.setShop_id(pet.getShop_id());
//        private Long address_id;

        return  orderAdopt;
    }
}
